package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.MallDeliveryAddressVo;
import com.quan.mall.entity.MallDeliveryAddress;
import com.quan.mall.mapper.MallDeliveryAddressMapper;
import com.quan.mall.service.MallDeliveryAddressService;


@Slf4j
@Service
public class MallDeliveryAddressServiceImpl extends MyBaseServiceImpl<MallDeliveryAddressMapper, MallDeliveryAddress> implements MallDeliveryAddressService {

    @Autowired
	private MallDeliveryAddressMapper mapper;

	@Override
    public List<MallDeliveryAddressVo> queryList(PageUtils pageUtils, MallDeliveryAddressVo vo) {
        QueryWrapper<MallDeliveryAddress> queryWrapper = new QueryWrapper<MallDeliveryAddress>(vo);

        List<MallDeliveryAddress> list = super.list(pageUtils, queryWrapper);
        List<MallDeliveryAddressVo> collect = list.stream().map(item -> {
            MallDeliveryAddressVo bean = new MallDeliveryAddressVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, MallDeliveryAddressVo vo) {
        QueryWrapper<MallDeliveryAddress> queryWrapper = new QueryWrapper<MallDeliveryAddress>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<MallDeliveryAddress> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<MallDeliveryAddress> records = page.getRecords();

        final List<MallDeliveryAddress> collect = records.stream().map((item) -> {
            MallDeliveryAddressVo bean = new MallDeliveryAddressVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public MallDeliveryAddress getDefaultAddress(Integer type) {
        QueryWrapper<MallDeliveryAddress> queryWrapper = new QueryWrapper<MallDeliveryAddress>();
        queryWrapper.eq("type", type);
        queryWrapper.eq("is_default", true);
        return super.getOne(queryWrapper);
    }

    @Override
    public boolean save(MallDeliveryAddress entity) {
        if (entity.getIsDefault()) { // 默认地址：0->否；1->是
            UpdateWrapper<MallDeliveryAddress> updateWrapper = new UpdateWrapper<MallDeliveryAddress>();
            updateWrapper.set("is_default", false);
            updateWrapper.eq("type", entity.getType());
            super.update(updateWrapper);
        }

        return super.save(entity);
    }

    @Override
    public boolean updateById(MallDeliveryAddress entity) {
        if (entity.getIsDefault()) { // 默认地址：0->否；1->是
            UpdateWrapper<MallDeliveryAddress> updateWrapper = new UpdateWrapper<MallDeliveryAddress>();
            updateWrapper.set("is_default", false);
            updateWrapper.eq("type", entity.getType());
            super.update(updateWrapper);
        }

        return super.updateById(entity);
    }
}